package com.util;

import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GzipUtil {

    private static final String charSet = "utf-8";


    /**
     * 将字符串压缩后Base64
     * @param primStr 待加压加密函数
     * @return
     */
    public static String gzipStringWithUrlEncode(String primStr) throws UnsupportedEncodingException {
        if (primStr == null || primStr.length() == 0) {
            return primStr;
        }
        return URLEncoder.encode(gzipString(primStr),charSet);
    }

    /**
     * 将字符串压缩后Base64
     * @param prims 待加压加密函数
     * @return
     */
    public static String gzipStringWithUrlEncode(byte[] prims) throws UnsupportedEncodingException {
        if (prims == null || prims.length == 0) {
            return "";
        }
        return URLEncoder.encode(gzipString(prims),charSet);
    }


    /**
     * 将字符串压缩后Base64
     * @param primStr 待加压加密函数
     * @return
     */
    public static String gzipString(String primStr) throws UnsupportedEncodingException {
        if (StringUtils.isEmpty(primStr)) {
            return primStr;
        }
        return gzipString(primStr.getBytes(charSet));
    }

    /**
     * 将字符串压缩后Base64
     * @param prims 待加压加密函数
     * @return
     */
    public static String gzipString(byte[] prims) throws UnsupportedEncodingException {
        if (prims == null || prims.length == 0) {
            return "";
        }
        ByteArrayOutputStream out = null;
        GZIPOutputStream gout = null;
        try{
            out = new ByteArrayOutputStream();
            gout = new GZIPOutputStream(out);
            gout.write(prims);
            gout.flush();
        } catch (IOException e) {
            System.out.println("对字符串进行加压加密操作失败");
            return null;
        } finally {
            if (gout != null) {
                try {
                    gout.close();
                } catch (IOException e) {
                    System.out.println("对字符串进行加压加密操作，关闭gzip操作流失败");
                }
            }
        }
        return new BASE64Encoder().encode(out.toByteArray());
    }

    /**
     * 将压缩并Base64后的字符串进行解密解压
     * @param compressedStr 待解密解压字符串
     * @return
     */
    public static final String ungzipString(String compressedStr) {
        if (StringUtils.isEmpty(compressedStr)) {
            return null;
        }
        ByteArrayOutputStream out = null;
        ByteArrayInputStream in = null;
        GZIPInputStream gin = null;
        String decompressed;
        try {
            byte[] compressed = new BASE64Decoder().decodeBuffer(compressedStr);
            out = new ByteArrayOutputStream();
            in = new ByteArrayInputStream(compressed);
            gin = new GZIPInputStream(in);
            byte[] buffer = new byte[1024];
            int offset = -1;
            while((offset = gin.read(buffer)) != -1) {
                out.write(buffer, 0, offset);
            }
            decompressed = out.toString(charSet);
        } catch (IOException e) {
            System.out.println("对字符串进行解密解压操作失败");
            decompressed = null;
        } finally {
            if (gin != null) {
                try {
                    gin.close();
                } catch (IOException e) {
                    System.out.println("对字符串进行解密解压操作，关闭压缩流失败");
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    System.out.println("对字符串进行解密解压操作，关闭输入流失败");
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    System.out.println("对字符串进行解密解压操作，关闭输出流失败");
                }
            }
        }
        return decompressed;
    }

    /**
     * 将压缩并Base64后的字符串进行解密解压
     * @param compressedStr 待解密解压字符串
     * @return
     */
    public static final String ungzipStringWithUrlDecode(String compressedStr) {
        if (StringUtils.isEmpty(compressedStr)) {
            return null;
        }
        String decompressedStr = null;
        try {
            decompressedStr = URLDecoder.decode(compressedStr,charSet);
        } catch (UnsupportedEncodingException e) {

        }
        return decompressedStr;
    }

    public static String fileToString(File input){
        InputStream fis = null;
        ByteArrayOutputStream baos = null;
        try{
            baos = new ByteArrayOutputStream();
            fis = new FileInputStream(input);
            byte[] buf = new byte[1024];
            int len=0;
            while((len=fis.read(buf))!=-1){
                baos.write(buf,0,len);
            }
            return new BASE64Encoder().encode(baos.toByteArray());
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                if(fis!=null) fis.close();
                if (baos!=null)baos.close();
            }catch(Exception e){

            }
        }
        return null;
    }


}
